#-*- coding:utf-8 -*-
import time
from wsgiref.simple_server import make_server
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

def home(params):
    return params

def login(params):
    return params

def index(params):
    return params

url = {
    '/':home,
    '/login':login,
    '/index':index,
} # 字典，键值对 键位路径，值位对应函数名字

def application(environ, start_response):
    path = environ.get('PATH_INFO') # 获取路径信息
    print(path)
    params = environ.get('QUERY_STRING') # 获取url请求"?"后边的参数
    print(params)

    # 如果路径为空 或 路径不在字典中
    if path is None or path not in url.keys():
        start_response('404 not found', [('Content-type', 'text/plain;charset=utf-8')])
        return [b'404 not found']
    
    # 获取键对应的值
    result = url.get(path)
    if result is None:
        start_response('404 not found', [('Content-type', 'text/plain;charset=utf-8')])
        return [b'404 not found']
    
    start_response('200 0K', [('Content-type', 'text/plain;charset=utf-8')])
    return [result(params).encode('utf-8')] 


    # if path == '/':
    #     start_response('200 0K', [('Content-type', 'text/plain;charset=utf-8')])
    #     return [b'/']
    # elif path == '/login':
    #     start_response('200 0K', [('Content-type', 'text/plain;charset=utf-8')])
    #     return [b'this is a login']
    # elif path == '/index':
    #     start_response('200 0K', [('Content-type', 'text/plain;charset=utf-8')])
    #     return [b'this is a index']
    # else:
    #     start_response('404 not found', [('Content-type', 'text/plain;charset=utf-8')])
    #     return [b'404 not found']
    
server = make_server('192.168.6.146',8080,application)
server.serve_forever()